package com.aixin.project.servlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.aixin.project.data.Course;
import com.aixin.project.data.Expert;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.util.Pagenation;

public class HallServlet extends BaseServlet{

	@Override
	protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
		List<Course> courses = CourseHandler.listComingCourse();
		if(null != courses && courses.size()>0){
			String pageno = request.getParameter("page");
			Pagenation page = new Pagenation();
			page.setPagenum(2);
			page.setTotalpage(courses.size()/2+courses.size()%2);
			if(null == pageno){
				page.setCurrentpage(1);
				page.setHasprev(false);
				page.setHasnext(courses.size() > page.getPagenum());
				if(null != courses){
					if(courses.size() < page.getPagenum()){
						page.setData(courses);
					}else{
						page.setData(courses.subList(0, page.getPagenum()));
					}
				}
			}else{
				int start = (Integer.parseInt(pageno)-1)*page.getPagenum();
				int end = Integer.parseInt(pageno)*page.getPagenum();
				if(end > courses.size())
					end = courses.size();
				page.setData(courses.subList(start, end));
				page.setCurrentpage(Integer.parseInt(pageno));
				if(Integer.parseInt(pageno) >1)
					page.setHasprev(true);
				else
					page.setHasprev(false);
				if(Integer.parseInt(pageno) >= page.getTotalpage())
					page.setHasnext(false);
				else
					page.setHasnext(true);
			}
			ctx.put("page", page);
		}

		List<Expert> experts = ExpertHandler.hallexpertList();
		if(null != experts && experts.size()>2)
			experts = experts.subList(0, 2);
		ctx.put("experts", experts);
		
		request.setAttribute("navhall", true);
		return getTemplate("hall.vm");
	}
	
	public static void main(String[] args) {
		int a=5;
		int b=2;
		System.out.println(a/b);
		System.out.println(a%b);
	}

}
